﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using zichan.BLL;
using zichan.Entity;
using MvcPaging;
using zichan.Models;

namespace zichan.Controllers
{
    public class MenuController : BaseController
    {
        // GET: Admin/Menu
        public ActionResult Index(string name, string cller, string act)
        {
            ViewBag.name = name;
            ViewBag.cller = cller;
            ViewBag.act = act;

            return View(MenuService.GetMenuList(name, cller, act, 1000, 1));
        }
        #region 添加
        public ActionResult Create(int? id)
        {
            MenuModel model = new MenuModel();
            if (id.HasValue && id != 0)
            {
                var m = MenuService.GetMenu(id.Value);
                if (m != null)
                    ViewBag.Ptile = m.Title;
            }
            else
                id = 0;
            model.Pid = id.Value;
            model.Sort = 1;
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MenuModel model)
        {
            if (!ModelState.IsValid) return View(model);
            Menu menu = new Menu();
            var p = MenuService.GetMenu(model.Pid);
            if (p != null)
            {
                var path = p.Path == "" ? "," : p.Path;
                menu.Path = path + p.Id + ",";
                menu.Level = p.Level + 1;
            }
            else
            {
                menu.Level = 0;
                menu.Path = "";
            }
            if (!string.IsNullOrEmpty(model.Controller))
                menu.Controller = model.Controller.ToLower();
            if (!string.IsNullOrEmpty(model.Action))
                menu.Action = model.Action.ToLower();
            menu.Ico = model.Ico;
            menu.Title = model.Title;
            menu.Sort = model.Sort;
            string err = MenuService.AddMenu(menu);
            if (string.IsNullOrEmpty(err))
                TempData["alert"] = "success#添加成功";
            else
                TempData["alert"] = "error#" + err;
            return RedirectToAction("index");
        }
        #endregion

        #region 编辑
        public ActionResult Edit(int id)
        {
            var m = MenuService.GetMenu(id);
            if (m == null)
                return HttpNotFound();

            return View(m);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Exclude = "Path,Level")]Menu model)
        {
            if (ModelState.IsValid)
            {
                var m = MenuService.GetMenu(model.Id);
                model.Path = m.Path;
                model.Level = m.Level;
                if (!string.IsNullOrEmpty(model.Controller))
                    model.Controller = model.Controller.ToLower();
                if (!string.IsNullOrEmpty(model.Action))
                    model.Action = model.Action.ToLower();
                MenuService.EditMenu(model);
                TempData["alert"] = "success#编辑成功";
                return RedirectToAction("index");
            }

            return View();
        }
        #endregion

        public ActionResult Delete(int id)
        {
            var m = MenuService.GetMenu(id);
            if (m == null)
                return HttpNotFound();

            return View(m);
        }

        [HttpPost]
        [ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult CmdDelete(int id)
        {
            MenuService.RemoveMenu(id);
            TempData["alert"] = "success#删除成功";

            return RedirectToAction("index");
        }
    }
}